package com.hyk.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyk.dto.ApiResult;
import com.hyk.entity.Product;
import com.hyk.entity.request.ProductRequest;
import com.hyk.entity.response.ProductResponse;
import com.hyk.service.IProductService;
import com.hyk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author hyk
 * @since 2021-03-18
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品控制类")
@CrossOrigin
public class ProductController {

    @Resource
    private IProductService productService;

    @PostMapping("/list")
    @ApiOperation("商品列表")
    public ApiResult list(@RequestBody ProductRequest request){
        IPage<ProductResponse> page = productService.findListForName(request);
        return R.ok(page);
    }

    @SneakyThrows
    @PostMapping("/add")
    @ApiOperation("添加商品")
    public ApiResult add(@RequestBody ProductRequest request){
        Product product = new Product();
        BeanUtils.copyProperties(request,product);
        product.setCategoryId(request.getCateId());
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        productService.save(product);
        return R.ok();
    }

    @PostMapping("upOrDown")
    @ApiOperation("上架或下架")
    public ApiResult upOrDown(@RequestBody ProductRequest request){
        productService.upOrDown(request.getId());
        System.out.println("下架成功！！！");
        return R.ok();
    }

}
